<?php
/**
 * Created by PhpStorm.
 * User: pf
 * Date: 2017/12/18
 * Time: 11:18
 */

namespace App\Repositories;



use App\Http\Controllers\Admin\Finance\Repository\Commission;
use App\Http\Controllers\Admin\Finance\Repository\Withdraw;
use App\Http\Controllers\Admin\Member\Repository\Member;
use App\Setting;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class UserRepository
{
    public function addGameNum($num=1){
        $user=Auth::guard('member')->user();
        $user->increment('game_num',$num);
    }
    //得到分销人数
    public function getGroupMember(Member $user){

       return Member::where('pid',$user->id)->paginate(15);
    }
    //分销人数记录
    public function getGroupCount(Member $user){
        return  Member::where('pid',$user->id)->count();
    }

    //我的套餐
    public function myCombo(Member $user){
       return DB::table('combo_orders')
            ->join('pay_orders','pay_orders.out_trade_no','=','combo_orders.outTradeNo')
            ->where('combo_orders.member_id','=',$user->id)
            ->where('pay_orders.is_pay',1)
            ->orderBy('combo_orders.id','desc')
           ->get();
    }
    public static function editInfo($post){
        $user=Auth::guard('member')->user();
        $repository = new Member();
        $model = $repository->find($user->id);
        if($model){
            $first = false;
            if(isset($post['mobile'])&&$post['mobile'])
                $model->mobile = (int)trim($post['mobile']);

            if(isset($post['nickname'])&&$post['nickname'])
                $model->nickname = $post['nickname'];
            if($model->info_complete==2){
                $model->info_complete=1;
                $first = true;
            }
            $model->id_number=$post['id_number'];
            $model->truename=$post['truename'];
            $return = $model->save();
            if($first&&$return){
                $distributModel = new DistributeRepository();
                $distributModel->account($user->id,DistributeRepository::ACT_FILL_INFO);
            }
            return $return;
        }else{
            return false;
        }
    }
    public function getRecord(){
      return   Commission::with(['member'=>function($query){
          return $query->select('id','nickname');
      }])->where('to_member',Auth::guard('member')->id())
          ->orderBy('id','desc')
          ->paginate(10);
    }
    //提现记录
    public function getPresentRecord(){
        return Withdraw::where('member_id',Auth::guard('member')->id())->paginate(10);
    }
    //提现记录总和
    public function getPresentRecordCount(){
        return Withdraw::where('member_id',Auth::guard('member')->id())->count();
    }

}
